package com.biz.service.basic;


import com.biz.model.Hmodel.basic.Tagent;
import com.biz.model.Pmodel.User;
import com.biz.model.Pmodel.api.OrderMain90;
import com.biz.model.Pmodel.basic.Pagent;
import com.biz.model.Pmodel.basic.PagentTree;
import com.biz.model.Pmodel.sys.Puser;
import com.biz.service.base.BaseServiceImpl;
import com.framework.dao.hdao.BaseDaoI;
import com.framework.dao.mdao.DaoSupport;
import com.framework.model.Paging;
import com.framework.model.Params;
import com.framework.utils.MD5;
import com.framework.utils.StringUtil;
import com.framework.utils.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by liujiajia on 2017/1/6.
 */
@Service("agentService")
public class AgentServiceImpl extends BaseServiceImpl<Tagent> implements AgentServiceI {

    @Resource(name = "daoSupport")
    private DaoSupport dao;
    @Autowired
    private BaseDaoI<Tagent> agentDao;

    @Override
    public Paging findAgentGrid(Params sqlParams) {
        Paging paging= dao.findForPagings("agentMapper.findAgentGrid",sqlParams,"agentMapper.findAgentCount",sqlParams);
        return paging;
    }

    @Override
    public List<PagentTree> getAgentListByUser(User user) throws Exception {
        return (List<PagentTree>) dao.findForList("agentMapper.getAgentListByUser",null);
    }

    @Override
    public void savePuser(Puser puser) throws Exception {
        String id = UuidUtil.get32UUID();
        puser.setId(id);
        String pwd = MD5.md5("123456");
        puser.setPwd(pwd);
        short idenity = 2;
        puser.setIdentity(idenity);
        if(StringUtil.isNullOrEmpty(puser.getProvince())){
            puser.setProvince("10");
        }
        if(StringUtil.isNullOrEmpty(puser.getCity())){
            puser.setCity("171");
        }
        if(StringUtil.isNullOrEmpty(puser.getArea())){
            puser.setArea("2112");
        }
        dao.save("agentMapper.savePuser",puser);
    }

    @Override
    public void updatePuser(Puser puser) throws Exception {
        if(StringUtil.isNullOrEmpty(puser.getProvince())){
            puser.setProvince("10");
        }
        if(StringUtil.isNullOrEmpty(puser.getCity())){
            puser.setCity("171");
        }
        if(StringUtil.isNullOrEmpty(puser.getArea())){
            puser.setArea("2112");
        }
        dao.save("agentMapper.updatePuser",puser);
    }

    @Override
    public String checkInfo(Puser puser) throws Exception {
        String A = "";
        if (!StringUtil.isNullOrEmpty(puser.getLoginName()))
        {
            A= "0";
        }
        return A;
    }

    @Override
    public String checkloginname(Puser puser) throws Exception {
        Integer loginName= (Integer) dao.findForObject("agentMapper.checkLoginName",puser);
        if(loginName>0)
        {return "2";}
        else
        {return "0";}
    }
    @Override
    public Puser findById(String id) throws Exception {
        Map<String,String> map=new HashMap<>();
        map.put("id",id);
        return (Puser) dao.findForObject("agentMapper.findById",map);
    }

    @Override
    public void delGridById(String ids) throws Exception {
        String [] idList=ids.split(",");
        for (String id:idList) {
          dao.delete("agentMapper.deletePuser",id);
        }
    }


    @Override
    public List<Pagent> getAgentListForSelect(User user) throws Exception {
        return (List<Pagent>) dao.findForList("agentMapper.getAgentListForSelect",user);
    }

    @Override
    public List<Map<String, Object>> queryBrandByAgent(String identity_code) throws Exception {
        return (List<Map<String, Object>>)dao.findForList("agentMapper.queryBrandByAgent",identity_code);
    }

    @Override
    public void saveOrderMain90(OrderMain90 order_main_90) throws Exception{
        dao.save("agentMapper.saveOrderMain90",order_main_90);
    }
}
